﻿/**
 * 
 * 
 * 
 * 商品管理 / 商品 列表
 * 
 * 
 */
define(['jquery', 'teplate', "text!temp/commodity/commodityList.html",
    "public/API", "text!temp/commodity/commodityListEach.html",
    "text!temp/commodity/addCList.html", "text!temp/commodity/guigeAPPend.html",
    "text!temp/commodity/addShuxin.html", "text!temp/commodity/noeListInfo.html",
    "ZeroClipboard",
    "formjs", "UEditor", "UEditorConf", "jquerypage"
], function ($, teplate, commodityListTpl, API, commodityListEachTpl, addCListTpl,
    guigeAPPendTpl, addShuxinTpl, noeListInfoTpl, ZeroClipboard) {
    return function () {
        window['ZeroClipboard'] = ZeroClipboard;

        var pagingUlNUm, flag = false; //返回页数
        var Httpurl = "";
        // 后改
        var datares = {};
        $.get(Httpurl + '/admin/goods/all', function (res) {
            datares.opselect = res;
            var $html1 = $(teplate.render(commodityListTpl, datares));
            //  点击添加 订单
            $html1.find('.but-addindent').on("click", function () {
                $.get(Httpurl + "/admin/goodsCategory/0", function (ress) {
                    var datares = {};
                    datares.data1 = ress;
                    $.get(Httpurl + "/admin/goodsType/allList", function (res) {
                        datares.data2 = res;
                        var $html = $(teplate.render(addCListTpl, datares));

                        // 动态改变 select的值1
                        $html.on("change", '.panrentsOption1', function () {
                            var top = $(this).val();
                            $.get(Httpurl + "/admin/goodsCategory/" + top, function (res) {
                                var $selectSecond = $html.find(".panrentsOptionTow1");
                                //.1、清除原来的二级分类数据
                                $selectSecond.empty();
                                //.2、添加新的二级分类数据
                                var option = "<option value=''>请选择商品属性</option>";
                                res.forEach(function (v) {
                                    option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                });
                                $selectSecond.append(option);
                                $html.find('.panrentsOptionTow1').change(function () {
                                    var top = $(this).val();

                                    $.get(Httpurl + '/admin/goodsCategory/' + top, function (res) {
                                        var $selectSecond = $html.find(".panrentsOption31");
                                        //.1、清除原来的二级分类数据
                                        $selectSecond.empty();
                                        //.2、添加新的二级分类数据
                                        var option = "<option value=''>请选择商品属性</option>";
                                        res.forEach(function (v) {
                                            option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                        });
                                        $selectSecond.append(option);

                                    })
                                })


                            })
                        })
                        // 动态改变 select的值2
                        $html.on("change", '.panrentsOption2', function () {
                            var top = $(this).val();
                            $.get(Httpurl + "/admin/goodsCategory/" + top, function (res) {
                                var $selectSecond = $html.find(".panrentsOptionTow2");
                                //.1、清除原来的二级分类数据
                                $selectSecond.empty();
                                //.2、添加新的二级分类数据
                                var option = "<option value=''>请选择商品属性</option>";
                                res.forEach(function (v) {
                                    option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                });
                                $selectSecond.append(option);
                                $html.find('.panrentsOptionTow2').change(function () {
                                    var top = $(this).val();

                                    $.get(Httpurl + '/admin/goodsCategory/' + top, function (res) {
                                        var $selectSecond = $html.find(".panrentsOption32");
                                        //.1、清除原来的二级分类数据
                                        $selectSecond.empty();
                                        //.2、添加新的二级分类数据

                                        var option = "<option value=''>请选择商品属性</option>";
                                        res.forEach(function (v) {
                                            option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                        });
                                        $selectSecond.append(option);

                                    })
                                })


                            })
                        })
                        $html.find('.but-return').on('click', function () {
                            $('.commodityList').click();
                        })

                        // 批量上传图片

                        var imgagsindex;
                        // 判断是否存在有值
                        $html.find('.butSouUploadFile').change(function () {
                            var li = $('.imageUrl').children('li:last').children('input').attr('name') || "";
                            if (li.indexOf('[') != -1) {
                                imgagsindex = parseInt((li.split('[')[1]).split(']')[0]);
                                imgagsindex++;
                            } else {
                                imgagsindex = 0;

                            }
                            var option = {
                                type: 'POST',
                                url: Httpurl + '/admin/goods/upload',
                                dataType: 'text',
                                data: {},
                                success: function (data) {
                                    var jsonObj = $.parseJSON(data);
                                    // 拼接图片添加
                                    var li = ' <li><img src="' + Httpurl + jsonObj.data + '" alt=""><input type="hidden" class="form-control " name="goodsImagesList[' + imgagsindex + '].imageUrl" value="' + jsonObj.data + '">\
                                     <button type="button" class="btn btn-danger but-remove">删除</button></li>'
                                    // 图片回显 显示图片
                                    $(li).appendTo($html.find('.imageUrl'));
                                    // 删除图片
                                    $('.but-remove').on('click', function () {
                                        $(this).parents('li').remove();
                                    })
                                }

                            };
                            $("#itemForm").ajaxSubmit(option);

                        })
                        // 商品 规格下拉框
                        $html.find('.panrentGuige').change(function () {
                            index = 0;
                            var top = $(this).val();
                            $.get(Httpurl + "/admin/spec/list?goodsTypeId=" + top, function (res) {
                                $html.find('.appendGuigexiangtbody').html(' ');
                                $html.find('.appguiGeTbody').html('');
                                if (res[0] == undefined) {
                                    return;
                                }
                                var $val = $(teplate.render(guigeAPPendTpl, res));

                                $val.find('button').on('click', function () {
                                    $(this).siblings().removeClass('btn-success');
                                    $(this).addClass('btn-success');
                                });
                                $html.find('.appguiGeTbody').html($val);
                            })
                        })
                        var indexs = $html.find('.appendGuigexiangtbody').children('tr').attr('data-index') || 0;
                        // 商品 规格下拉框 确定按钮
                        $html.find('.ensure').on('click', function () {
                            var $chirend = $($(this).siblings('ul.appguiGeTbody').children('.guigeTr'));
                            // 下面为添加这符串
                            var theadhtml = '';
                            var tbodyhtml = '';
                            var inputs = '';
                            var arrs = [];
                            var inputs2 = '';
                            for (var i = 0; i < $chirend.length; i++) {
                                var content = $($chirend[i]).children('button.btn-success').text();
                                var ids = $($chirend[i]).children('button.btn-success').attr('data-id');
                                var parnedcontent = $($chirend[i]).children('b.lastchiren').text();
                                inputs += parnedcontent + ":" + content + " ";
                                //                            inputs2 += ids + "_";
                                arrs.push(ids);
                                theadhtml += "<th>" + parnedcontent + "</th>";
                                tbodyhtml += "<td>" + content + "</td>";
                            }
                            arrs.sort(function (a, b) {
                                return a - b;
                            })
                            inputs2 = arrs.join('_');
                            var $val1 = '<input type="hidden" name="specGoodsPriceList[' + indexs + '].keyName" value="' + inputs + '">';
                            var $val2 = '<input type="hidden" name="specGoodsPriceList[' + indexs + '].key" value="' + inputs2 + '">';
                            var remove = '<td><button type="button" class="btn btn-danger but-remove">删除</button></td>'
                            $html.find('.appendGuigexiangtable').html(theadhtml + "<th>价格</th><th>库存</th><th>SKU</th><th>操作</th>");
                            $("<tr data-index='" + indexs + "'>" + tbodyhtml + "<td><input type='text' name='specGoodsPriceList[" + indexs + "].price' ></td><td><input type='text' class='but-PriceList' name='specGoodsPriceList[" + indexs + "].storeCount' ></td><td><input type='text' name='specGoodsPriceList[" + indexs + "].sku' >" + $val1 + $val2 + " </td>" + remove + "</tr>").appendTo($html.find('.appendGuigexiangtbody'));
                            indexs++;
                            inputs2 = "";
                            inputs = "";
                            $('.but-remove').on('click', function () {
                                $(this).parents('tr').remove();
                            })
                            $('.but-PriceList').change(function () {
                                var input = $('.but-PriceList');
                                var readonlyVal = 0;
                                for (var i = 0; i < input.length; i++) {

                                    readonlyVal += parseInt($(input[i]).val());
                                }
                                $('.but-readonly').val(readonlyVal);
                            });
                        });
                        // 商品 属性下拉框
                        $html.find('.panrentsOptionshuxin').change(function () {
                            var id = $(this).val();
                            $.get(Httpurl + "/admin/goodsAttribute/list/" + id, function (res) {
                                var $shuxin = $(teplate.render(addShuxinTpl, res));
                                $html.find('.panrentsOptionshuxintbody').html($shuxin)
                            })
                        })
                        // 获取富文本内容;
                        var content;
                        // 点击提交
                        $html.on('submit', "form", function () {
                            var formData = $(this).serialize();
                            $.post(Httpurl + "/admin/goods/add", formData, function (res) {
                                if (res.status == 200) {
                                    alert('添加成功哈');
                                    $('.commodityList').click();
                                } else {
                                    alert('添加失败');
                                }
                            })
                            return false;
                        })
                        $('.right').html($html);
                        // 上传图片
                        $('.butUploadFile').change(function () {
                            var option = {
                                type: 'POST',
                                url: Httpurl + '/admin/goods/upload',
                                dataType: 'text',
                                data: {},
                                success: function (data) {
                                    var jsonObj = $.parseJSON(data);
                                    $html.find('.addUploadFile').val(jsonObj.data);
                                    $('.butUploadFile').remove();
                                    var input = '<input type="file" class="form-control butUploadFile" name="uploadFile" value="上传图片">';
                                    $(input).insertBefore('.addUploadFile');
                                }
                            };
                            $("#itemForm").ajaxSubmit(option);
                        })
                        // // ue编辑器
                        UE.delEditor('container');
                        var ue = UE.getEditor('container');
                    })

                })
            });
            // 分类联动
            $.get(Httpurl + "/admin/goodsCategory/0", function (res) {
                var option = "";
                res.forEach(function (v) {
                    option += "<option value='" + v.id + "'>" + v.name + "</option>";
                });
                $html1.find('.Firstcategory').append(option);
                $html1.find('.Firstcategory').change(function () {
                    $html1.find('.SecondClassification').empty();
                    $html1.find('.ThirdClassification').empty();
                    var val = $(this).val();
                    $.get(Httpurl + "/admin/goodsCategory/" + val, function (res) {
                        var option = '<option value="" selected="selected">第二分类</option>';
                        res.forEach(function (v) {
                            option += "<option value='" + v.id + "'>" + v.name + "</option>";
                        });
                        $html1.find('.SecondClassification').append(option);
                    })
                })
                $html1.find('.SecondClassification').change(function () {
                    var val = $(this).val();
                    $.get(Httpurl + "/admin/goodsCategory/" + val, function (res) {
                        var option = '<option value="" selected="selected">第三分类</option>';
                        res.forEach(function (v) {
                            option += "<option value='" + v.id + "'>" + v.name + "</option>";
                        });
                        $html1.find('.ThirdClassification').html(option);
                    })
                })
            })
            // 点击筛选
            $html1.on('submit', "form", function () {
                var formData = $(this).serialize();
                API.commodityList(formData, function (res) {
                    if (res.data) {
                        var $html = $(teplate.render(commodityListEachTpl, res))
                        $("#pagenumtbdoy").html($html);
                        API.pageFun($('.page'), res.totalPage, callback, "/admin/goods/list?pageNum=", "&" + formData);
                        if (flag) {
                            setTimeout(function () {
                                $(".pagesmunber").val(pagingUlNUm);
                                $('.turndown').click();
                                flag = false;
                            }, 500)
                        }
                    } else {
                        $('#pagenumtbdoy').empty();
                        API.pageFun($('.page'), 1);
                    }
                })
                return false;
            });
            $('.right').html($html1);
            // 保存 下拉框选择状态
            $(".Status").change(function () {
                $(this).children(":selected").attr("selected", "selected").siblings().removeAttr('selected');
            })
            // 分页 设置
            var callback = function (n, url, formData) {
                $('.pageNum').css('display', "block");
                $.get(Httpurl + url + n + formData, function (res) {
                    var $html = $(teplate.render(commodityListEachTpl, res));
                    // 点击编辑
                    $html.find('.but-compile').on('click', function () {
                        var $right = $('.home-right').clone(true);
                        pagingUlNUm = $('.current').text() - 0; // 页数
                        var id = $(this).parents('td').attr('data-id');
                        $.get(Httpurl + "/admin/goods/getById/" + id, function (ress) {
                            var datares = {};
                            datares.data1 = ress;
                            $.get(Httpurl + "/admin/goodsCategory/0", function (res) {
                                datares.data2 = res;
                                $.get(Httpurl + "/admin/goodsType/allList", function (res) {
                                    datares.data3 = res;
                                    var $html = $(teplate.render(noeListInfoTpl, datares));
                                    // 动态改变 select的值1
                                    $html.on("change", '.panrentsOption1', function () {
                                        var top = $(this).val();
                                        $.get(Httpurl + "/admin/goodsCategory/" + top, function (res) {
                                            var $selectSecond = $html.find(".panrentsOptionTow1");
                                            //.1、清除原来的二级分类数据
                                            $selectSecond.empty();
                                            //.2、添加新的二级分类数据
                                            var option = "<option value=''>请选择商品属性</option>";
                                            res.forEach(function (v) {
                                                option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                            });
                                            $selectSecond.append(option);
                                            $html.find('.panrentsOptionTow1').change(function () {
                                                var top = $(this).val();

                                                $.get(Httpurl + '/admin/goodsCategory/' + top, function (res) {
                                                    var $selectSecond = $html.find(".panrentsOption31");
                                                    //.1、清除原来的二级分类数据
                                                    $selectSecond.empty();
                                                    //.2、添加新的二级分类数据

                                                    var option = "<option value=''>请选择商品属性</option>";
                                                    res.forEach(function (v) {
                                                        option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                                    });
                                                    $selectSecond.append(option);

                                                })
                                            })


                                        })
                                    });
                                    // 动态改变 select的值2

                                    $html.on("change", '.panrentsOption2', function () {
                                        var top = $(this).val();
                                        $.get(Httpurl + "/admin/goodsCategory/" + top, function (res) {
                                            var $selectSecond = $html.find(".panrentsOptionTow2");
                                            //.1、清除原来的二级分类数据
                                            $selectSecond.empty();
                                            //.2、添加新的二级分类数据
                                            var option = "<option value=''>请选择商品属性</option>";
                                            res.forEach(function (v) {
                                                option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                            });
                                            $selectSecond.append(option);
                                            $html.find('.panrentsOptionTow2').change(function () {
                                                var top = $(this).val();

                                                $.get(Httpurl + '/admin/goodsCategory/' + top, function (res) {
                                                    var $selectSecond = $html.find(".panrentsOption32");
                                                    //.1、清除原来的二级分类数据
                                                    $selectSecond.empty();
                                                    //.2、添加新的二级分类数据
                                                    var option = "<option value=''>请选择商品属性</option>";
                                                    res.forEach(function (v) {
                                                        option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                                    });
                                                    $selectSecond.append(option);
                                                })
                                            })


                                        })
                                    });

                                    // 点击返回
                                    $html.find('.but-return').on('click', function () {
                                        $('.right').empty();
                                        flag = true;
                                        // 分类联动
                                        $.get(Httpurl + "/admin/goodsCategory/0", function (res) {
                                            var option = "";
                                            res.forEach(function (v) {
                                                option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                            });
                                            $right.find('.Firstcategory').append(option);
                                            $right.find('.Firstcategory').change(function () {
                                                $right.find('.SecondClassification').empty();
                                                $right.find('.ThirdClassification').empty();
                                                var val = $(this).val();
                                                $.get(Httpurl + "/admin/goodsCategory/" + val, function (res) {
                                                    var option = '<option value="" selected="selected">第二分类</option>';
                                                    res.forEach(function (v) {
                                                        option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                                    });
                                                    $right.find('.SecondClassification').append(option);
                                                })
                                            })
                                            $right.find('.SecondClassification').change(function () {
                                                var val = $(this).val();
                                                $.get(Httpurl + "/admin/goodsCategory/" + val, function (res) {
                                                    var option = '<option value="" selected="selected">第二分类</option>';
                                                    res.forEach(function (v) {
                                                        option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                                    });
                                                    $right.find('.ThirdClassification').html(option);
                                                })
                                            })
                                        })
                                        $('.right').html($right);
                                        $(".but-seek").click();
                                    });
                                    // 批量上传图片
                                    var imgagsindex;
                                    $html.find('.butSouUploadFile2').change(function () {
                                        // 判断是否存在有值
                                        var li = $('.imageUrl').children('li:last').children('input').attr('name') || "";
                                        if (li.indexOf('[') != -1) {
                                            imgagsindex = parseInt((li.split('[')[1]).split(']')[0]);
                                            imgagsindex++;
                                        } else {
                                            imgagsindex = 0;
                                        }
                                        var option = {
                                            type: 'POST',
                                            url: Httpurl + '/admin/goods/upload',
                                            dataType: 'text',
                                            data: {},
                                            success: function (data) {
                                                var jsonObj = $.parseJSON(data);
                                                // 拼接图片添加
                                                var li = ' <li><img src="' + Httpurl + jsonObj.data + '" alt=""><input type="hidden" class="form-control " name="goodsImagesList[' + imgagsindex + '].imageUrl" value="' + jsonObj.data + '">\
                                                    <button type="button" class="btn btn-danger but-remove">删除</button></li>';
                                                // 图片回显 显示图片
                                                var $li = $(li);
                                                // 删除图片
                                                $li.find('.but-remove').on('click', function () {

                                                    $(this).parents('li').remove();
                                                });
                                                $li.appendTo($html.find('.imageUrl'));
                                                // $('<img src="' + Httpurl + jsonObj.data + '" height="100" width="100" alt=""> <input type="hidden" class="form-control addUploadFile" name="goodsImagesList[' + imgagsindex + '].imageUrl" value="' + jsonObj.data + '">').insertBefore($html.find('.filetu'));
                                                imgagsindex++;
                                            }

                                        };
                                        $("#itemForm").ajaxSubmit(option);

                                    })

                                    // 进去就调用一次属性类型
                                    var GuingVal = $html.find('.panrentGuige').val();
                                    if (GuingVal != '') {
                                        $.get(Httpurl + "/admin/spec/list?goodsTypeId=" + GuingVal, function (res) {
                                            $html.find('.appendGuigexiangtbody').html(' ');
                                            var $val = $(teplate.render(guigeAPPendTpl, res));

                                            $val.find('button').on('click', function () {
                                                $(this).siblings().removeClass('btn-success');
                                                $(this).addClass('btn-success');
                                            });
                                            $html.find('.appguiGeTbody').html($val);
                                        })
                                    }
                                    // 商品 规格下拉框
                                    $html.find('.panrentGuige').change(function () {
                                        index = 0;
                                        var top = $(this).val();
                                        $.get(Httpurl + "/admin/spec/list?goodsTypeId=" + top, function (res) {
                                            $html.find('.appendGuigexiangtbody').html(' ');
                                            var $val = $(teplate.render(guigeAPPendTpl, res));

                                            $val.find('button').on('click', function () {
                                                $(this).siblings().removeClass('btn-success');
                                                $(this).addClass('btn-success');
                                            });
                                            $html.find('.appguiGeTbody').html($val);
                                        })

                                    })
                                    var index2;
                                    // 商品 规格下拉框 确定按钮
                                    $html.find('.ensure').on('click', function () {

                                        var yuanIndex = $html.find('.yuanIndex').children('tr:last').attr('data-index');
                                        var xinzhen = $html.find('.appendGuigexiangtbody').children('tr:last').attr('data-index');
                                        if (yuanIndex == undefined && index2 == undefined && xinzhen == undefined) {
                                            index2 = yuanIndex || -1;
                                            index2++;

                                            // 判断是否新添加是否有值
                                        } else if (xinzhen != undefined) {
                                            index2 = parseInt(xinzhen);
                                            index2++;

                                        } else {
                                            index2 = parseInt(yuanIndex);
                                            index2++;
                                        }

                                        var $chirend = $($(this).siblings('ul.appguiGeTbody').children('.guigeTr'));
                                        // 下面为添加这符串
                                        var theadhtml = '';
                                        var tbodyhtml = '';
                                        var inputs = '';
                                        var arrs = [];
                                        var inputs2 = '';
                                        for (var i = 0; i < $chirend.length; i++) {
                                            var content = $($chirend[i]).children('button.btn-success').text();
                                            var ids = $($chirend[i]).children('button.btn-success').attr('data-id');
                                            var parnedcontent = $($chirend[i]).children('b.lastchiren').text();
                                            inputs += parnedcontent + ":" + content + " ";
                                            // inputs2 += ids + "_";
                                            arrs.push(ids);
                                            theadhtml += "<th>" + parnedcontent + "</th>";
                                            tbodyhtml += "<td>" + content + "</td>";
                                        }
                                        arrs.sort(function (a, b) {
                                            return a - b;
                                        })
                                        inputs2 = arrs.join('_');

                                        var $val1 = '<input type="hidden" name="specGoodsPriceList[' + index2 + '].keyName"  value="' + inputs + '">';
                                        var $val2 = '<input type="hidden" name="specGoodsPriceList[' + index2 + '].key" value="' + inputs2 + '">';
                                        var $val3 = '<input type="hidden" name="specGoodsPriceList[' + index2 + '].goodsId" value="' + datares.data1.goods.goodsId + '">'
                                        var remove = '<td><button type="button" class="btn btn-danger but-remove">删除</button></td>'
                                        $html.find('.appendGuigexiangtable').html(theadhtml + "<th>价格</th><th>库存</th><th>SKU</th><th>操作</th>");
                                        $("<tr data-index='" + index2 + "'>" + tbodyhtml + "<td><input type='text' name='specGoodsPriceList[" + index2 + "].price' ></td><td><input type='text' class='but-PriceList' name='specGoodsPriceList[" + index2 + "].storeCount' ></td><td><input type='text' name='specGoodsPriceList[" + index2 + "].sku' >" + $val1 + $val2 + $val3 + " </td>" + remove + "</tr>").appendTo($html.find('.appendGuigexiangtbody'));
                                        index2++;
                                        inputs2 = "";
                                        inputs = "";
                                        $('.but-remove').on('click', function () {

                                            $(this).parents('tr').remove();
                                        })
                                        $('.but-PriceList').change(function () {
                                            var input = $('.but-PriceList');
                                            var readonlyVal = 0;
                                            for (var i = 0; i < input.length; i++) {

                                                readonlyVal += parseInt($(input[i]).val());
                                            }
                                            $('.but-readonly').val(readonlyVal);
                                        })
                                    });
                                    // 商品 属性下拉框
                                    $html.find('.panrentsOptionshuxin').change(function () {
                                        var id = $(this).val();
                                        $.get(Httpurl + "/admin/goodsAttribute/list/" + id, function (res) {
                                            var $shuxin = $(teplate.render(addShuxinTpl, res));
                                            $html.find('.panrentsOptionshuxintbody').html($shuxin)
                                        })
                                    })
                                    // 删除
                                    $html.find('.but-remove').on('click', function () {
                                        $(this).parents('li').remove();
                                    });
                                    // 删除原规格项
                                    $html.find('.but-remove2').on('click', function () {
                                        $(this).parents('tr').remove();
                                    })
                                    // 点击提交
                                    $html.on('submit', "form", function () {
                                        var formData = $(this).serialize();
                                        $.post(Httpurl + "/admin/goods/update", formData, function (res) {
                                            if (res.status == 200) {
                                                alert('修改成功哈');
                                                $('.right').empty();
                                                flag = true;
                                                // 分类联动
                                                $.get(Httpurl + "/admin/goodsCategory/0", function (res) {
                                                    var option = "";
                                                    res.forEach(function (v) {
                                                        option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                                    });
                                                    $right.find('.Firstcategory').append(option);
                                                    $right.find('.Firstcategory').change(function () {
                                                        $right.find('.SecondClassification').empty();
                                                        $right.find('.ThirdClassification').empty();
                                                        var val = $(this).val();
                                                        $.get(Httpurl + "/admin/goodsCategory/" + val, function (res) {
                                                            var option = '<option value="" selected="selected">第二分类</option>';
                                                            res.forEach(function (v) {
                                                                option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                                            });
                                                            $right.find('.SecondClassification').append(option);
                                                        })
                                                    })
                                                    $right.find('.SecondClassification').change(function () {
                                                        var val = $(this).val();
                                                        $.get(Httpurl + "/admin/goodsCategory/" + val, function (res) {
                                                            var option = '<option value="" selected="selected">第二分类</option>';
                                                            res.forEach(function (v) {
                                                                option += "<option value='" + v.id + "'>" + v.name + "</option>";
                                                            });
                                                            $right.find('.ThirdClassification').html(option);
                                                        })
                                                    })
                                                })
                                                $('.right').html($right);
                                                $(".but-seek").click();
                                            } else {
                                                alert('修改失败');
                                            }
                                        })
                                        return false;
                                    })
                                    // 替换富文本值
                                    var container = $html.find('.pUEcontent').text();
                                    $('.right').html($html);
                                    // 给物流判断添加;
                                    var chirent = $("#inputchecked").children('input').length;
                                    var arr = [];
                                    var str = ["12", '13', '16'];
                                    var children = $("#inputchecked").children('input');
                                    for (var i = 0; i < chirent; i++) {
                                        var val = $(children[i]).val();
                                        arr.push(val);
                                        for (var j = 0; j < str.length; j++) {
                                            if (arr[i] == str[j]) {
                                                str.splice(j, 1);
                                            }
                                        }
                                    };
                                    var input = "";
                                    for (var i = 0; i < str.length; i++) {
                                        if (str[i] == "12") {
                                            input += '<input type="checkbox"  name="goods.shippingAreaIds" value="12">&nbsp;&nbsp;&nbsp;<span> UAE</span>&nbsp;&nbsp;&nbsp;';
                                        } else if (str[i] == "13") {
                                            input += '<input type="checkbox"  name="goods.shippingAreaIds" value="13">&nbsp;&nbsp;&nbsp;<span>Saudi</span>&nbsp;&nbsp;&nbsp;';
                                        } else if (str[i] == "16") {
                                            input += '<input type="checkbox" name="goods.shippingAreaIds" value="16">' + "&nbsp;&nbsp;<span>Oman</span>&nbsp;&nbsp;";
                                        }
                                    };
                                    $(input).appendTo("#inputchecked");
                                    // 上传图片
                                    $('.butUploadFile').change(function () {
                                        var option = {
                                            type: 'POST',
                                            url: Httpurl + '/admin/goods/upload',
                                            dataType: 'text',
                                            data: {},
                                            success: function (data) {
                                                var jsonObj = $.parseJSON(data);
                                                $html.find('.addUploadFile').val(jsonObj.data);
                                                $('.butUploadFile').remove();
                                                var input = '<input type="file" class="form-control butUploadFile" name="uploadFile" value="上传图片">';
                                                $(input).insertBefore('.addUploadFile');
                                            }

                                        };
                                        $("#itemForm").ajaxSubmit(option);
                                    })
                                    $('#container').html(container);
                                    // 动态修改库存
                                    var input = $('.but-PriceList');
                                    var readonlyVal = 0;
                                    if (input.length >= 1) {
                                        for (var i = 0; i < input.length; i++) {
                                            readonlyVal += parseInt($(input[i]).val());
                                        }
                                    } else {
                                        readonlyVal = datares.data1.goods.storeCount;
                                    }
                                    $('.but-readonly').val(readonlyVal);
                                    $('.but-PriceList').change(function () {
                                        var input = $('.but-PriceList');
                                        var readonlyVal = 0;
                                        for (var i = 0; i < input.length; i++) {
                                            readonlyVal += parseInt($(input[i]).val());
                                        }
                                        $('.but-readonly').val(readonlyVal);
                                    })
                                    // // ue 富文本编辑器
                                    UE.delEditor('container');
                                    var ue = UE.getEditor('container');
                                })
                            })
                        })
                    })
                    // 更改图片
                    $html.find('.butimages').on('click', function (e) {
                        var images = $(e.target);
                        var id = $(this).parents('tr').attr('data-id')
                        var isshow = $(this).attr('data-show');
                        var isshowhede = isshow.split(',');
                        var that = $(this);
                        $.get(Httpurl + "/admin/goods/isShowAndOrder?goodsId=" + id + "&" + isshowhede[0] + "=" + isshowhede[1], function (res) {
                            if (res.status == 200) {
                                if (res.data == true) {

                                    images.attr('src', "./images/xianshi.png");
                                    that.attr('data-show', isshowhede[0] + ",true")
                                } else {
                                    images.attr('src', "./images/yincang.png");
                                    that.attr('data-show', isshowhede[0] + ",false");
                                }
                            }
                        })

                    })
                    // 修改表单的值 (外面)
                    $html.find('input.conditionModification').change(function () {
                        var value = $(this).val();
                        var id = $(this).parents('tr').attr('data-id')
                        var isshow = $(this).parents('td').attr('data-show');
                        $.get(Httpurl + "/admin/goods/isShowAndOrder?goodsId=" + id + "&" + isshow + "=" + value, function (res) {
                            if (res.status == 200) {
                                alert('修改成功')
                            } else {
                                alert('修改失败');
                            }
                        })


                    })
                    // 删除
                    $html.find('.but-remove').on("click", function (e) {
                        var id = $(this).parents('td').attr("data-id");
                        var that = $(this);
                        if (confirm('确定删除吗?')) {
                            $.ajax({
                                url: Httpurl + "/admin/goods/delete?goodId=" + id,
                                success: function (res) {
                                    if (res.status == 200) {
                                        alert("删除成功");
                                        that.parents('tr').remove();
                                    } else {
                                        alert('删除失败')
                                    }
                                }
                            })
                        }

                    })
                    $('#pagenumtbdoy').html($html);
                })
            }
            // 自调用一次
            $(".but-seek").click();
        })
    }
})